﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using BidFunLib.Entity;
using System.Diagnostics;

namespace BidFunLib.Services
{
    /// <summary>
    /// 
    /// </summary>
    public class BidOperatorTypeManager
    {
        /// <summary>
        /// Logger declaration
        /// </summary>
        private static readonly ILog log = LogManager.GetLogger(typeof(BidOperatorTypeManager));

        /// <summary>
        /// Type allowed for searching BidOperatorTypeById
        /// </summary>
        public enum BID_OPERATOR_TYPE {NONE, MANUAL, AUTOMATIC}

        /// <summary>
        /// Singleton instance
        /// </summary>
        private static BidOperatorTypeManager instance;

        /// <summary>
        /// Retreive singleton of this class
        /// </summary>
        public static BidOperatorTypeManager Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new BidOperatorTypeManager();
                }
                return BidOperatorTypeManager.instance;
            }
            set
            {
                BidOperatorTypeManager.instance = value;
            }
        }

        /// <summary>
        /// The dao of this manager
        /// </summary>
        private BidOperatorTypeDAO dao = new BidOperatorTypeDAO();

        /// <summary>
        /// Predefined None type in order to load it from database only once.
        /// </summary>
        public BidOperatorType OperatorTypeNone { get; set; }

        /// <summary>
        /// Predefined Manual type in order to load it from database only once.
        /// </summary>
        public BidOperatorType OperatorTypeManual { get; set; }

        /// <summary>
        /// Predefined Automatic type in order to load it from database only once.
        /// </summary>
        public BidOperatorType OperatorTypeAutomatic { get; set; }

        
        /// <summary>
        /// private constructor. Singleton pattern.
        /// </summary>
        private BidOperatorTypeManager()
        {
            OperatorTypeNone = dao.getBidOperatorTypeById(BID_OPERATOR_TYPE.NONE);
            if (OperatorTypeNone == null)
            {
                OperatorTypeNone = dao.saveBidOperatorType(new BidOperatorType(BID_OPERATOR_TYPE.NONE.ToString()));
            }
            OperatorTypeManual = dao.getBidOperatorTypeById(BID_OPERATOR_TYPE.MANUAL);
            if (OperatorTypeManual == null)
            {
                OperatorTypeManual = dao.saveBidOperatorType(new BidOperatorType(BID_OPERATOR_TYPE.MANUAL.ToString()));
            }
            OperatorTypeAutomatic = dao.getBidOperatorTypeById(BID_OPERATOR_TYPE.AUTOMATIC);
            if (OperatorTypeAutomatic == null)
            {
                OperatorTypeAutomatic = dao.saveBidOperatorType(new BidOperatorType(BID_OPERATOR_TYPE.AUTOMATIC.ToString()));
            }
        }

        /// <summary>
        /// return the user corresponding to the pseudo
        /// search is perfomed in memory in the user list who have bet during this session
        /// </summary>
        /// <param name="pseudo"></param>
        /// <returns></returns>
        public BidOperatorType getBidOperatorTypeByType(BID_OPERATOR_TYPE type)
        {
            BidOperatorType bidOperatorType = OperatorTypeNone;

            if (type == BID_OPERATOR_TYPE.MANUAL)
            {
                bidOperatorType = OperatorTypeManual;
            }
            else if (type == BID_OPERATOR_TYPE.AUTOMATIC)
            {
                bidOperatorType = OperatorTypeAutomatic;
            }

            return bidOperatorType;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public BidOperatorType getBidOperatorTypeByID(Int32 id)
        {
            BidOperatorType bidOperatorType = OperatorTypeNone;

            if (id.Equals(BID_OPERATOR_TYPE.MANUAL))
            {
                bidOperatorType = OperatorTypeManual;
            }
            else if (id.Equals(BID_OPERATOR_TYPE.AUTOMATIC))
            {
                bidOperatorType = OperatorTypeAutomatic;
            }

            return bidOperatorType;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public BidOperatorType getBidOperatorTypeByName(String name)
        {
            if(name.ToLower().Contains("manu"))
            {
                return OperatorTypeManual;
            
            }
            else if (name.ToLower().Contains("auto"))
            {
                return OperatorTypeAutomatic;
            }
            else
            {
                return OperatorTypeNone;
            }
        }
    }
}
